package model

import (
	"context"
	"fmt"
	"github.com/zeromicro/go-zero/core/stores/sqlx"
)

var _ PmsProductAttrValueModel = (*customPmsProductAttrValueModel)(nil)

type (
	// PmsProductAttrValueModel is an interface to be customized, add more methods here,
	// and implement the added methods in customPmsProductAttrValueModel.
	PmsProductAttrValueModel interface {
		pmsProductAttrValueModel
		FindProductDetail(ctx context.Context, categoryId, spuId int64) ([]ProductDetailResult, error)
	}

	customPmsProductAttrValueModel struct {
		*defaultPmsProductAttrValueModel
	}
)

func (c *customPmsProductAttrValueModel) FindProductDetail(ctx context.Context, categoryId, spuId int64) ([]ProductDetailResult, error) {
	query := fmt.Sprintf(`SELECT pms_attr_group.attr_group_name,pms_attr.attr_name, pms_product_attr_value.attr_value
FROM pms_attr  
INNER JOIN pms_attr_attrgroup_relation ON pms_attr.attr_id = pms_attr_attrgroup_relation.attr_id
INNER JOIN pms_attr_group ON pms_attr_attrgroup_relation.attr_group_id = pms_attr_group.attr_group_id
LEFT JOIN pms_product_attr_value ON pms_product_attr_value.attr_id = pms_attr.attr_id
WHERE pms_attr.catelog_id = %d AND pms_product_attr_value.spu_id = %d`, categoryId, spuId)
	var resp []ProductDetailResult
	err := c.conn.QueryRowsCtx(ctx, &resp, query)
	return resp, err
}

// NewPmsProductAttrValueModel returns a model for the database table.
func NewPmsProductAttrValueModel(conn sqlx.SqlConn) PmsProductAttrValueModel {
	return &customPmsProductAttrValueModel{
		defaultPmsProductAttrValueModel: newPmsProductAttrValueModel(conn),
	}
}

type ProductDetailResult struct {
	AttrGroupName string `json:"attr_group_name" db:"attr_group_name"`
	AttrName      string `json:"attr_name" db:"attr_name"`
	AttrValue     string `json:"attr_value" db:"attr_value"`
}
